﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Web;
using System.Web.Mvc;
using Website.Infrastructure;
using Website.Interfaces;

namespace IlseLeijten.Tests
{
    [TestClass]
    public class RedirectAttributeTests
    {
        [TestMethod]
        public void NoRedirectIfNotRequired()
        {
            // Arrange
            var redirectManager = new Mock<IRedirectionManager>(MockBehavior.Strict);
            redirectManager.Setup(x => x.RedirectionRequired(It.IsAny<HttpRequestBase>()))
                .Returns(()=> false);
            
            var filterAttrib = new RedirectFilterAttribute(redirectManager.Object);

            var httpRequest = new Mock<HttpRequestBase>();

            var httpContext = new Mock<HttpContextBase>();
            httpContext.SetupGet(x => x.Request)
                .Returns(httpRequest.Object);

            // can't mock ActionExecutingContext, so instantiate a real object
            // and set the RequestContext.HttpContext property to a mock object
            var execContext = new ActionExecutingContext();
            execContext.RequestContext.HttpContext = httpContext.Object;

            // Act
            filterAttrib.OnActionExecuting(execContext);

            // Assert
            redirectManager.Verify(x=>x.RedirectionRequired(It.IsAny<HttpRequestBase>()));
        }
    }
}
